package com.lc.pianostudent.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.pianostudent.BaseApplication;
import com.lc.pianostudent.R;
import com.lc.pianostudent.conn.PostGetuser;
import com.lc.pianostudent.conn.PostUploadAvatar;
import com.lc.pianostudent.dialog.PictureDialog;
import com.lc.pianostudent.fragment.MineFragment;
import com.lc.pianostudent.util.PictureUtil;
import com.lc.pianostudent.util.Util;
import com.lc.pianostudent.view.RoundImageView;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.permission.PermissionsActivity;
import com.zcx.helper.util.UtilToast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2018/6/25.
 */

public class PersonInfoActivity extends BasePictureActivity implements View.OnClickListener{

    @BoundView(value = R.id.personal_info_head,isClick = true) private RelativeLayout personal_info_head;
    @BoundView(value = R.id.personal_info_nickname_layout,isClick = true) private LinearLayout personal_info_nickname_layout;
    @BoundView(value = R.id.personal_info_remark_layout,isClick = true) private LinearLayout personal_info_remark_layout;

    @BoundView(R.id.personal_info_image) private CircleImageView personal_info_image_image;
    @BoundView(R.id.personal_info_truename) private TextView personal_info_truename;
    @BoundView(R.id.personal_info_sex) private TextView personal_info_sex;
    @BoundView(R.id.personal_info_born) private TextView personal_info_born;
    @BoundView(R.id.personal_info_remark) private TextView personal_info_remark;
    @BoundView(R.id.personal_info_nickname) private TextView personal_info_nickname;
    private String nick = "";
    private String remark = "";
    @BoundView(value = R.id.tele_phone,isClick = true) private TextView tele_phone;
    private int degree_int = 0;
    private Bitmap bitmap;
    //修改头像
    public PostUploadAvatar postUploadAvatar = new PostUploadAvatar(new AsyCallBack<PostUploadAvatar.PostUploadAvatarInfo>(){


        @Override
        public void onSuccess(String toast, int type, Object o, PostUploadAvatar.PostUploadAvatarInfo postUploadAvatarInfo) throws Exception {

            GlideLoader.getInstance().get(this,postUploadAvatarInfo.data , personal_info_image_image,R.mipmap.new_head);
            BaseApplication.BasePreferences.saveHeadImg(postUploadAvatarInfo.data);
            try {
                ((MineFragment.CallBack)getAppCallBack(MineFragment.class)).onRefresh();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });

    //个人信息
    private PostGetuser postGetuser = new PostGetuser(new AsyCallBack<PostGetuser.PostGetuserInfo>() {


        @Override
        public void onSuccess(String toast, int type, Object o, PostGetuser.PostGetuserInfo postGetuserInfo) throws Exception {

            personal_info_truename.setText(postGetuserInfo.cname);
            GlideLoader.getInstance().get(postGetuserInfo.avatar,personal_info_image_image,R.mipmap.new_head);
            if(postGetuserInfo.sex.equals("1")){
                personal_info_sex.setText("男");
            }else {
                personal_info_sex.setText("女");
            }
            personal_info_born.setText(postGetuserInfo.year+"年"+postGetuserInfo.mouse+"月");
            personal_info_remark.setText(postGetuserInfo.person);
            personal_info_nickname.setText(postGetuserInfo.name);
            nick= postGetuserInfo.name;
            remark = postGetuserInfo.person;
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_info);

        setBackTrue();

        setTitleName("个人信息");

        initView();
    }

    private void initView(){
        tele_phone.setText(BaseApplication.BasePreferences.readKefu());
        setAppCallBack(new CallBack());
        postGetuser.user_id = BaseApplication.BasePreferences.readUID();
        postGetuser.type = "0";
        postGetuser.execute();
    }
    @Override
    protected void resultPhotoPath(ImageView imageView, String s) {
//        String path_ys = PictureUtil.saveBitmap(PersonInfoActivity.this, PictureUtil.compressImage(PictureUtil.getSmallBitmap(s)));
//        防止图片翻转
        degree_int =  Util.readPictureDegree(s);
        bitmap = Util.toturn(PictureUtil.getimage(s),degree_int);
        String path_ys = PictureUtil.saveBitmap(PersonInfoActivity.this, PictureUtil.compressImage(bitmap));

//        String path_ys = PictureUtil.saveBitmap(PersonInfoActivity.this, PictureUtil.compressImage(PictureUtil.getimage(s)));
        postUploadAvatar.user_id = BaseApplication.BasePreferences.readUID();
        postUploadAvatar.image = new File(path_ys);
        postUploadAvatar.execute();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.personal_info_head:
                action();
                break;

            case R.id.personal_info_nickname_layout:

                startActivity(new Intent(PersonInfoActivity.this,EditNicknameActivity.class).putExtra("nick",nick));
                break;

            case R.id.personal_info_remark_layout:

                startActivity(new Intent(PersonInfoActivity.this,RemarkActivity.class).putExtra("remark",remark));

                break;
        }
    }

    private void action(){
        PermissionsActivity.StartActivity(new String[]{Manifest.permission.CAMERA}, new PermissionsActivity.PermissionsCallBack() {

            @Override
            public void onSuccess() {

                new PictureDialog(context){

                    @Override
                    public void onAlbum() {

                        startAlbum(null);

                    }

                    @Override
                    public void onCamera() {

                        startCamera(null);

                    }

                }.show();

            }

        });
    }

    public class CallBack implements AppCallBack {

        public void onRefresh() {
            postGetuser.user_id = BaseApplication.BasePreferences.readUID();
            postGetuser.type = "0";
            postGetuser.execute();

        }

    }


}
